﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Icoders.MarketTracker.Core.MasterData;
using Icoders.MarketTracker.Core.Repository.MasterDataRepositories.UserRepositories;
using Icoders.MarketTracker.Core.Validation;
using Icoders.MarketTracker.Data.EF;
using Icoders.MarketTracker.Data.Utils;

namespace Icoders.MarketTracker.Data.Repository.MasterDataRepositories.UserRepositories
{
    public class UserGroupRoleRepository :RepositoryBase, IUserGroupRoleRepository
    {
         private AppDataContext _ctx;
         public UserGroupRoleRepository(AppDataContext ctx)
       {
           _ctx = ctx;
       }
        public ValidationResultInfo Validate(UserGroupRole objToValidate)
        {
            ValidationResultInfo vri = objToValidate.BasicValidation();
           
            return vri;
        }

        public int Save(UserGroupRole entity)
        {

            ValidationResultInfo vri = Validate(entity);
            if (!vri.IsValid)
            {
                throw new DomainValidationException(vri, "User group Details provided not Valid");
            }
            DateTime date = TimeUtil.GetDateTime();
            TblUserGroupRole tbl = _ctx.TblUserGroupRole.FirstOrDefault(s => s.Id == entity.Id);
            if (tbl == null)
            {
                tbl = new TblUserGroupRole();
                tbl.CreatedOn = date;
                tbl.StatusId = (int)MasterEntityStatus.New;
                _ctx.TblUserGroupRole.AddObject(tbl);


            }
            tbl.UpdatedOn = date;
            tbl.RoleId = entity.RoleId;
            tbl.UserGroupId = entity.GroupId;
            tbl.CanAcces = entity.CanAcces;
            _ctx.SaveChanges();
            return tbl.Id;
        }

        public UserGroupRole GetById(int id)
        {
            var tbl = _ctx.TblUserGroupRole.FirstOrDefault(s => s.Id == id);
            if (tbl == null) return null;
            return Map(tbl);
        }

        private UserGroupRole Map(TblUserGroupRole tbl)
        {
            return new UserGroupRole
            {
                Id = tbl.Id,
                GroupId = tbl.UserGroupId,
                RoleId=tbl.RoleId,
                UpdatedOn = tbl.UpdatedOn,
                CreatedOn = tbl.CreatedOn,
                CanAcces=tbl.CanAcces,
                Status = (MasterEntityStatus)tbl.StatusId
            };
        }

        public List<UserGroupRole> GetAll()
        {
            return _ctx.TblUserGroupRole.ToList().Select(n => Map(n)).ToList();
        }

        public void Delete(int id)
        {
            throw new NotImplementedException();
        }

        public UserGroupPlaceHolder GetPlaceHolder(int groupID)
        {
             var group = _ctx.TblUserGroup.FirstOrDefault(f => f.Id == groupID);
            if(group==null)
               throw new Exception("User group not Valid");
            UserGroupPlaceHolder placeholder = new UserGroupPlaceHolder();
            MakePlaceholder(groupID);
            List<UserGroupRoleView> Rows = _ctx.TblUserGroupRole.Where(p => p.UserGroupId == groupID).Select(s=>new UserGroupRoleView{CanAccess=s.CanAcces,Id=s.Id,RoleId=s.RoleId,RoleName=s.TblRole.Description}).ToList();
           
            placeholder.Rows = Rows;
            placeholder.GroupName = group.Name;
            placeholder.GroupId = group.Id;
            return placeholder;
        }

        private void MakePlaceholder(int GroupId)
        {
            DateTime date = TimeUtil.GetDateTime();
            foreach(var role in _ctx.TblRole)
            {
                var groupRole =_ctx.TblUserGroupRole.FirstOrDefault(p => p.UserGroupId == GroupId && p.RoleId == role.Id);
                if(groupRole==null)
                {
                    TblUserGroupRole tbl = new TblUserGroupRole
                                               {
                                                   RoleId = role.Id,
                                                   StatusId = 0,
                                                   UserGroupId = GroupId,
                                                   CreatedOn = date,
                                                   UpdatedOn = date,
                                                   CanAcces = false,
                                               };
                    _ctx.TblUserGroupRole.AddObject(tbl);


                }
            }
            _ctx.SaveChanges();
        }

        protected override string _cacheKey
        {
            get { throw new NotImplementedException(); }
        }

        protected override string _cacheListKey
        {
            get { throw new NotImplementedException(); }
        }
    }
}
